package com.example.room_demo

import androidx.room.RoomDatabase
import androidx.room.Room
import androidx.room.Database
import android.content.Context


@Database(entities = [DataBean::class], version = 3, exportSchema = false)
abstract class DataBaseManager : RoomDatabase() {

    abstract fun dataDao(): DataDao

    //获取单例
    companion object {

        @JvmStatic
        @Volatile
        private var INSTANCE: DataBaseManager? = null

        @JvmStatic
        fun getDatabase(context: Context): DataBaseManager {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    DataBaseManager::class.java, "data_database"
                )
                    .addMigrations(Migrations1_2())//增加一个数据字段
                    .addMigrations(Migrations2_3())//修改数据表字段名称
                    .build()
                INSTANCE = instance
                return instance
            }
        }
    }
}